package com.tadpole.music.view.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.tadpole.music.R;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.ToastUtils;

import razerdp.basepopup.BasePopupWindow;

/**
 * Created By 孙岩
 * on 2020/9/24
 */
public class PurchasePopup extends BasePopupWindow implements View.OnClickListener {
    //文本
    private TextView tvText;
    //9.9
    private TextView tvOne;
    //498
    private TextView tvTwo;
    //关闭
    private ImageView ivClose;
    private IModeSelection iModeSelection;
    //套题价格
    private String suit_price;

    @SuppressLint("SetTextI18n")
    public PurchasePopup(Activity context, String suit_price) {
        super(context);
        this.suit_price = suit_price;
        tvText = (TextView) findViewById(R.id.tvText);
        tvOne = (TextView) findViewById(R.id.tvOne);
        tvTwo = (TextView) findViewById(R.id.tvTwo);
        ivClose = (ImageView) findViewById(R.id.ivClose);
        ivClose.setOnClickListener(this);
        tvOne.setOnClickListener(this);
        tvTwo.setOnClickListener(this);
        tvOne.setText(suit_price+"购买");

        String source = "请选择" + suit_price + "元购买或享受\nVIP498元全部训练";

        SpannableString spannable = new SpannableString(source);

        //这个一定要记得设置，不然点击不生效
        tvText.setMovementMethod(LinkMovementMethod.getInstance());
        tvText.setHighlightColor(Color.TRANSPARENT);
        //用户协议
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (Mutils.isNetworkAvailable()) {

                } else {
                    ToastUtils.show("请检查您的网络设置");
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setLinearText(false);
            }
        }, 3, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置文字的前景色
        spannable.setSpan(getRedSpan(context), 9 + suit_price.length(), 17 + suit_price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(getRedSpan(context), 3, suit_price.length() + 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvText.setText(spannable);
    }

    private ForegroundColorSpan getRedSpan(Activity activity) {
        return new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.color_red));
    }


    @Override
    protected Animation initShowAnimation() {
        return null;
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.pop_purchase);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.rlPurchase);
    }

    public void setModeListener(IModeSelection iModeSelection) {
        this.iModeSelection = iModeSelection;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvOne:
                iModeSelection.getMode(1);
                break;
            case R.id.tvTwo:
                iModeSelection.getMode(2);
                break;
            case R.id.ivClose:
                iModeSelection.getMode(3);
                break;
        }
    }

    public interface IModeSelection {
        void getMode(int mode);
    }
}
